#!python3

"""
xlwings使用
https://blog.csdn.net/whalefall/article/details/102665002

必须要安装office才能使用
"""

import xlwings as xw


def readExcel(filePath):
    # 打开Excel程序，默认设置：程序不可见，只打开不新建工作薄，屏幕更新关闭
    app = xw.App(visible=False, add_book=False)
    app.display_alerts = False
    app.screen_updating = False

    wb = app.books.open(filePath)  # 打开excel文档
    # wb = app.books.add()  # 新建excel文档

    # 返回工作簿的绝对路径
    print(wb.fullname)
    # 返回工作簿的名称
    print(wb.name)

    # 引用工作簿中的sheet
    # sheet = wb.sheets['sheet1']
    sheet = wb.sheets['sheet名']
    # sheets = wb.sheets # 获取所有sheet
    # sheet = wb.sheets.active# 引用活动sheet

    # range = sheet
    # range = sheet.range('A1')  # 引用A1单元格
    range = sheet['A1:B5']  # A1:B5单元格
    # range = sheet[0, 1]  # B1单元格
    # range = sheet[:10, :10]  # A1:J10

    print(wb.name, sheet.name, range.value, sep="\n")

    wb.save()  # 打开excel文档
    # wb.save(filePath)  # 新建excel文档
    wb.close()
    app.quit()  # 结束程序


if __name__ == '__main__':
    path = 'F:\\Download\\2020年.xlsx'
    readExcel(path)
